package com.yike.user.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.lkl.laop.sdk.exception.SDKException;
import com.yike.common.Response.LklApplyLedgerReceiverResponse;
import com.yike.common.api.Add;
import com.yike.common.api.ApiResult;
import com.yike.common.api.Update;
import com.yike.common.base.BaseController;
import com.yike.common.constant.RocketMqConstant;
import com.yike.common.entity.ShopSplit;
import com.yike.common.entity.SysInfo;
import com.yike.common.enums.OperationLogType;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.pagination.Paging;
import com.yike.common.request.ApplyForSepAccReq;
import com.yike.common.tool.StringUtil;
import com.yike.common.vo.ShopSplitVo;
import com.yike.user.lkl.api.V2SeparateAccounts;
import com.yike.user.lkl.incoming.IncomingApi;
import com.yike.user.lkl.incoming.RsaDecrypt;
import com.yike.user.lkl.incoming.push.MerchantAudit;
import com.yike.user.lkl.incoming.push.MerchantModification;
import com.yike.user.lkl.incoming.request.IncomingPush;
import com.yike.user.mapper.SysInfoMapper;
import com.yike.user.param.PlatformShopPageParam;
import com.yike.user.param.PlatformShopParam;
import com.yike.user.service.PlatformShopService;
import com.yike.user.vo.PlatformShopVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 平台店铺 控制器
 *
 * @author Sakura
 * @since 2024-10-31
 */
@Slf4j
@RestController
@RequestMapping("/platform/shop")
@Module("user")
@Api(value = "平台店铺API", tags = {"平台店铺管理"})
public class PlatformShopController extends BaseController {

    @Autowired
    private PlatformShopService platformShopService;

    @Autowired
    private IncomingApi incomingApi;

    @Autowired
    private V2SeparateAccounts  v2SeparateAccounts;
    @Autowired
    private SysInfoMapper sysInfoMapper;


//    @PostMapping("/cjjsf")
//    public String   cjjsf() throws Exception {
//        SysInfo  sysInfo = sysInfoMapper.selectOne(new LambdaQueryWrapper<SysInfo>().last("limit 1"));
//        LklApplyLedgerReceiverResponse lklApplyLedgerReceiverResponse = v2SeparateAccounts.applyLedgerReceiver(StringUtil.randomDateNumOr8(), sysInfo);
//        sysInfo.setReceiverNo(lklApplyLedgerReceiverResponse.getReceiverNo());
//        sysInfoMapper.updateById(sysInfo);
//        return null;
//    }
//
//    @PostMapping("/getInfoxx")
//    public String   getInfoxx(@RequestParam String customerNo) throws Exception {
//       Map<String ,Object> merchantInfo = incomingApi.getMerchantInfo(customerNo);
//        if(ObjectUtils.isNotEmpty(merchantInfo) && merchantInfo.containsKey("customer")){
//            Map<String,Object> customer = (Map<String,Object>) merchantInfo.get("customer");
//            if(ObjectUtils.isNotEmpty(customer)){
//                return (String) customer.get("externalCustomerNo");
//            }
//        }
//        return null;
//    }
    /**
     * 店铺申请开通分账
     * @param req
     * @return
     */
    @PostMapping("/applyForSepAcc")
    @OperationLog(name = "店铺申请开通分账", type = OperationLogType.ADD)
    @ApiOperation(value = "店铺申请开通分账", response = ApiResult.class)
    public ApiResult<String>  applyForSepAcc(@Validated @RequestBody ApplyForSepAccReq req) throws SDKException, IOException {
        return platformShopService.applyForSepAcc(req);
    }

    @PostMapping("/getApplyForSepAcc")
    @OperationLog(name = "店铺获取分账资料详情", type = OperationLogType.ADD)
    @ApiOperation(value = "店铺获取分账资料详情", response = ShopSplitVo.class)
    public ApiResult<ShopSplitVo>  getApplyForSepAcc(@Validated @RequestParam(value = "shopId") String shopId) throws SDKException, IOException {
        return platformShopService.getApplyForSepAcc(shopId);
    }


    /**
     * 店铺分账申请绑定接收方
     * @param shopId
     * @return
     */
    @PostMapping("/applyBind")
    @OperationLog(name = "店铺手动申请绑定分账接收方", type = OperationLogType.ADD)
    @ApiOperation(value = "店铺手动申请绑定分账接收方", response = ApiResult.class)
    public ApiResult<String>  applyBind(@RequestParam(value = "shopId") String shopId) throws SDKException, IOException {
        return platformShopService.applyBind(shopId);
    }

    /**
     * 添加店铺
     */
    @PostMapping("/add")
    @OperationLog(name = "添加店铺", type = OperationLogType.ADD)
    @ApiOperation(value = "添加店铺", response = ApiResult.class)
    public ApiResult<String> addShop(@Validated(Add.class) @RequestBody PlatformShopParam platformShopParam) throws Exception {
        return platformShopService.saveShop(platformShopParam);
    }

    /**
     * 修改店铺
     */
    @PostMapping("/update")
    @OperationLog(name = "修改店铺", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改店铺", response = ApiResult.class)
    public ApiResult<String> updateShop(@Validated(Update.class) @RequestBody PlatformShopParam platformShopParam) throws Exception {
        return platformShopService.updateShop(platformShopParam);
    }

    /**
     * 删除店铺
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除店铺", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除店铺", response = ApiResult.class)
    public ApiResult<Boolean> deleteShop(@PathVariable("id") Long id) throws Exception {
        boolean flag = platformShopService.deleteShop(id);
        return ApiResult.result(flag);
    }

    @PostMapping("/batchDel")
    @OperationLog(name = "批量删除店铺", type = OperationLogType.UPDATE)
    @ApiOperation(value = "批量删除店铺", response = ApiResult.class)
    public ApiResult<Boolean> batchDel(@Validated(Update.class) @RequestBody List<Long> ids) throws Exception {
        boolean flag = platformShopService.batchDel(ids);
        return ApiResult.result(flag);
    }

    /**
     * 店铺详情
     */
    @GetMapping("/info/{id}")
    @OperationLog(name = "店铺详情", type = OperationLogType.INFO)
    @ApiOperation(value = "店铺详情", response = PlatformShopVo.class)
    public ApiResult<PlatformShopVo> getShop(@PathVariable("id") Long id) throws Exception {
        PlatformShopVo platformShopVo = platformShopService.getShop(id);
        return ApiResult.ok(platformShopVo);
    }

    @PostMapping("/getPageList")
    //@OperationLog(name = "商户分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "店铺分页列表", response = PlatformShopVo.class)
    public ApiResult<Paging<PlatformShopVo>> getShopPageList(@Validated @RequestBody PlatformShopPageParam platformShopPageParam) throws Exception {
        Paging<PlatformShopVo> paging = platformShopService.getShopPageList(platformShopPageParam);
        return ApiResult.ok(paging);
    }


}

